package com.example.observepattern.observer.impl;

import com.example.observepattern.observer.Observer;
import com.example.observepattern.subject.Subject;

import java.util.ArrayList;
import java.util.List;

//气象站实现被观察者接口
public class WeatherStation implements Subject {

    private List<Observer> observers; // 观察者列表
    private float temperature;//温度
    private float humidity;//湿度
    private float pressure;//气压
    public WeatherStation() {
        observers = new ArrayList<>();
    }

    @Override
    public void registryObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(temperature, humidity, pressure);
        }
    }

    // 气象站更新数据（核心业务）
    public void setMeasurements(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        System.out.println("\n【气象站】数据更新：温度=" + temperature + "°C, 湿度=" + humidity + "%, 气压=" + pressure);

    //    气象站数据更新，立刻通知其他订阅者
        notifyObservers();
    }
}
